//
//  EndAddressChooseVC.m
//  saler-ios
//
//  Created by 王亚陆 on 17/6/5.
//  Copyright © 2017年 sayimba. All rights reserved.
//

#import "EndAddressChooseVC.h"
#import "AddressCollectionCell.h"
#import "LeftAddressCell.h"
@interface EndAddressChooseVC ()
<UITableViewDelegate,
UITableViewDataSource,
UICollectionViewDelegate,
UICollectionViewDataSource
>
{
    UIImageView * nav;
    UITableView * Table;
    RootViewController * TabBar;
    NSMutableDictionary * callBackDic;
    NSMutableArray * leftArrData;
    NSMutableArray * rightArrData;
    
    UICollectionView * conllectionView;
    
    int Left;
    NSMutableArray * leftStatueArr;

}
@property (nonatomic,strong) UIView * backview;
@end

@implementation EndAddressChooseVC


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.begainID = [[NSString alloc]init];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    nav = [self createNav:@"选择目的地"];
    [self.view addSubview:nav];
    self.view.backgroundColor = UIviewBackColor;
    
    leftStatueArr = [[NSMutableArray alloc]init];
    
    Left = 0;
//    leftArrData = [NSMutableArray arrayWithObjects:@"西安",@"广州",@"川渝起运",@"武汉", nil];
//    rightArrData = [NSMutableArray arrayWithObjects:@[@"巴中",@"巴中",@"巴中",@"巴中",@"巴中",@"巴中",@"巴中",@"巴中"],@[@"西安",@"西安",@"西安",@"西安",@"西安",@"西安",@"西安"],@[@"广州",@"广州"],@[@"武汉"],nil];
//    rightArrData = [NSMutableArray arrayWithObjects:@"巴中",@"巴中",@"巴中",@"巴中",@"巴中",@"巴中",@"巴中",@"巴中",nil];

    
    
    _backview = [[UIView alloc]init];
    _backview.backgroundColor = WhiteColor;
    [self.view addSubview:_backview];
    [_backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(nav.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, Main_height-64-10-10));
    }];

    
    UILabel * shengLabel = [UILabel createUIlabel:@"目的省" andFont:FontOfSize13 andColor:RGBACOLOR(139, 139, 139, 1)];
    [_backview addSubview:shengLabel];
    [shengLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_backview.mas_left).with.offset(45*kWidth);
        make.top.mas_equalTo(_backview.mas_top).with.offset(15*kHeight);
    }];
    
    UIImageView * image1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Destination"]];
    [_backview addSubview:image1];
    [image1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(shengLabel.mas_left).with.offset(-5*kWidth);
        make.centerY.mas_equalTo(shengLabel.mas_centerY);
    }];

    
    
    
    
    
    
    UILabel * shiLabel = [UILabel createUIlabel:@"目的市" andFont:FontOfSize13 andColor:RGBACOLOR(139, 139, 139, 1)];
    [_backview addSubview:shiLabel];
    [shiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_backview.mas_right).with.offset(-125*kWidth);
        make.top.mas_equalTo(_backview.mas_top).with.offset(15*kHeight);
    }];
    
    
    UIImageView * image2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Destination"]];
    [_backview addSubview:image2];
    [image2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(shiLabel.mas_left).with.offset(-5*kWidth);
        make.centerY.mas_equalTo(shiLabel.mas_centerY);
    }];

    
  
    callBackDic = [[NSMutableDictionary alloc]init];
    Table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, 86*kHeight) style:UITableViewStylePlain];
    [Table setTableFooterView:[UIView new]];
    Table.delegate = self;
    Table.dataSource = self;
    Table.sectionIndexBackgroundColor= GrayColor;
    [_backview addSubview:Table];
    [Table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backview.mas_left);
        make.top.mas_equalTo(shiLabel.mas_bottom).with.offset(5*kHeight);
        make.size.mas_equalTo(CGSizeMake(90*kWidth, Main_height-64-10-10-10-10));
    }];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(90*kWidth , 38*kHeight );

    conllectionView =  [[UICollectionView alloc]initWithFrame:CGRectMake(0, cellHeight * 7 + 20, screenWidth,  (cellHeight + 130 )) collectionViewLayout:layout];
    conllectionView.backgroundColor = WhiteColor;
    conllectionView.delegate = self;
    conllectionView.dataSource = self;
    [conllectionView registerNib:[UINib nibWithNibName:@"AddressCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];

    [self.view addSubview:conllectionView];
    [conllectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_backview.mas_right);
        make.top.mas_equalTo(shiLabel.mas_bottom).with.offset(5*kHeight);
        make.size.mas_equalTo(CGSizeMake(250*kWidth, Main_height-64-10-10));
    }];


    [self createDate];
    
    
}

-(void)createDate{
    
    NSString *urlString = [NSString stringWithFormat:@"%@",Order_provincearea];
    
    
    [Common requestWithUrlString:urlString contentType:application_json finished:^(id responseObj) {
        
        
        
        NSLog(@"%@",responseObj[@"message"]);
        if ([responseObj[@"success"] boolValue]) {
            
            leftArrData = responseObj[@"data"];
            
            for (int i = 0; i<leftArrData.count; i++) {
                
                NSString * yesColor = nil;
                if (i==0) {
                    yesColor = @"yes";
                }else{
                    yesColor = @"no";
                }
                [leftStatueArr addObject:yesColor];
            }
            
            [Table reloadData];
            
            [self createRightDate:leftArrData[0][@"id"]];
            
        } else {
            
            [WKProgressHUD popMessage:responseObj[@"message"] inView:self.view duration:1.5 animated:YES];
            
        }
        
        
    } failed:^(NSString *errorMsg) {
        
        NSLog(@"%@",errorMsg);
        
    }];
  
}

-(void)createRightDate:(NSString*)RigthID{
    
    
    
    NSString *urlString = [NSString stringWithFormat:@"%@/%@",Order_destarea,RigthID];
    
    [Common requestWithUrlString:urlString contentType:application_json finished:^(id responseObj) {
        
        
        NSLog(@"%@",responseObj[@"message"]);
        if ([responseObj[@"success"] boolValue]) {
            
            NSMutableArray * arr = [[NSMutableArray alloc]init];
            arr = responseObj[@"data"];
            rightArrData = arr.mutableCopy;
            
            if (rightArrData.count >0) {
                [conllectionView reloadData];
            }
        } else {
            
            [WKProgressHUD popMessage:responseObj[@"message"] inView:self.view duration:1.5 animated:YES];
        }
        
    } failed:^(NSString *errorMsg) {
        
        NSLog(@"%@",errorMsg);
        
    }];

    
    
    
}



#pragma mark - 表单协议

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section{
    
    return leftArrData.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *string = @"fontCell";
    
    LeftAddressCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        cell = [[LeftAddressCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    [Table selectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];

    
//    {
//        code = 440000;
//        id = 440000;
//        initialWords = gds;
//        level = province;
//        name = "\U5e7f\U4e1c\U7701";
//        parentCode = "<null>";
//        path = 440000;
//        pinyin = guangdongsheng;
//        sname = "\U5e7f\U4e1c\U7701";
//        status = 10;
//    },

    
    cell.AddressName.text = leftArrData[indexPath.row][@"sname"];
    cell.AddressName.font = Font(FontOfSize12);
    

    if ([leftStatueArr[indexPath.row] isEqualToString:@"yes"]) {
        
        cell.AddressName.textColor = YellowColor;
        cell.LineImage.hidden = NO;
    }else{
        cell.AddressName.textColor = TitleColor;
        cell.LineImage.hidden = YES;
    }
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return  CellHeight;
    
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    
    
    for (int i = 0; i<leftStatueArr.count; i++) {
        
        [leftStatueArr replaceObjectAtIndex:i withObject:@"no"];

    }
    [leftStatueArr replaceObjectAtIndex:indexPath.row withObject:@"yes"];
    
    [Table reloadData];
    
    Left = (int)indexPath.row;
    [self createRightDate:leftArrData[indexPath.row][@"id"]];
    

    
}


#pragma mark UICollectionView Delegate DataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return rightArrData.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    static NSString * cellIde=@"cell";
    //因为提前注册了cell CollectionView一旦从重用队列中拿不到cell对象，会自动创建对应样式的cell
    AddressCollectionCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellIde forIndexPath:indexPath];
    cell.backgroundColor = WhiteColor;

    cell.addressLabel.text = rightArrData[indexPath.item][@"sname"];
    cell.layer.borderWidth = 0.5;
    cell.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;


    return cell;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    return CGSizeMake(110*kWidth , 33*kHeight );
}




-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 5, 10, 10);
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]initWithDictionary:rightArrData[indexPath.item]];
    
    NSString * leftStr = [NSString stringWithFormat:@"%@·%@",leftArrData[Left][@"sname"],rightArrData[indexPath.item][@"sname"]];
    
    [dic setObject:leftStr forKey:@"sname"];
    
    if (_callBackAddress) {
        _callBackAddress(dic);
    }
    
    NSLog(@"%ld",(long)indexPath.item);
    [self.navigationController popViewControllerAnimated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
